﻿package com.sohu.sns.avatar {
	import flash.utils.ByteArray;

	import com.adobe.images.JPGEncoder;
	import com.adobe.serialization.json.JSON;

	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;

	public class BinaryTransfer extends EventDispatcher {
		private var pId : String = "";
		private var postUrl : String;
		private var msgStr : String = "default";
		private var from : String;
		private var photoServer : String;
		private var type : String;
		public static var ERROR : String = "error";
		public static var COMPLETE : String = "complete";

		public function BinaryTransfer(postUrl : String, type : String, photoServer : String = "", from : String = "", param5 : String = "") {
			this.postUrl = postUrl;
			this.type = type;
			this.photoServer = photoServer;
			this.from = from;
			this.postUrl = this.postUrl + ("&type=" + this.type);
			this.postUrl = this.postUrl + ("&photoServer=" + this.photoServer);
			this.postUrl = this.postUrl + ("&from=" + this.from);
			this.postUrl = this.postUrl + ("&photoId=" + param5);
		}

		public function get msg() : String {
			return this.msgStr;
		}

		public function get pServer() : String {
			return this.photoServer;
		}

		private function onIOError(event : IOErrorEvent) : void {
			this.msgStr = "io error";
			this.dispatchEvent(new Event(ERROR));
		}

		private function onSecurityError(event : SecurityErrorEvent) : void {
			this.msgStr = "securityError:" + event.toString();
			this.dispatchEvent(new Event(ERROR));
		}

		public function get photoId() : String {
			return this.pId;
		}

		private function onComplete(event : Event) : void {
			var loader : URLLoader = URLLoader(event.target);
			loader.dataFormat = URLLoaderDataFormat.VARIABLES;
			try {
				var arr : Object = JSON.decode(String(loader.data));
				if (arr['status'] == 1) {
					if (arr['data'] == null) {
						this.msgStr = "no response";
						this.dispatchEvent(new Event(ERROR));
					} else {
						this.msgStr = arr['statusText'];
						if (arr['data']['urls']) {
							// this.photoServer = arr['data']['large'];
							this.photoServer = arr['data']['urls'][0];
						}
						if (arr['photoId']) {
							this.pId = arr['photoId'];
						} else {
							this.pId = "";
						}
						this.dispatchEvent(new Event(COMPLETE));
					}
				} else {
					this.msgStr = arr['statusText'];
					this.dispatchEvent(new Event(ERROR));
				}
			} catch (e : Error) {
				this.msgStr = "json format error";
				this.dispatchEvent(new Event(ERROR));
			}
		}

		public function transferData(bitmapData : BitmapData) : void {
			var jpgEncoder : JPGEncoder = new JPGEncoder(100);
			var bytes : ByteArray = jpgEncoder.encode(bitmapData);
			var quest : URLRequest = new URLRequest(this.postUrl);
			quest.data = bytes;
			quest.method = URLRequestMethod.POST;
			quest.contentType = "application/octet-stream";
			var loader : URLLoader = new URLLoader();
			loader.load(quest);
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		}
	}
}
